package com.sub.project.info.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;

import com.sub.common.utils.StringUtils;
import com.sub.project.info.domain.Honor;
import com.sub.project.info.service.IHonorService;
import com.sub.project.info.vo.RestVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.common.utils.poi.ExcelUtil;
import com.sub.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-01-07
 */
@RestController
@RequestMapping("/system/honor" )
public class HonorController extends BaseController {
    @Autowired
    private IHonorService honorService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:honor:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(Honor honor) {
        startPage();
        QueryWrapper<Honor> queryWrapper = new QueryWrapper<Honor>();
                    if (StringUtils.isNotBlank(honor.getHonorName())){
            queryWrapper.like("honor_name" ,honor.getHonorName());
        }
            if(honor.getGetTime() != null){
     queryWrapper.eq("get_time" ,honor.getGetTime());
    }

        if(StringUtils.isNotBlank(honor.getOpen_id())){
            queryWrapper.eq("open_id" ,honor.getOpen_id());
        }
        if (StringUtils.isNotBlank(honor.getGetDeptName())){
            queryWrapper.like("get_dept_name" ,honor.getGetDeptName());
        }
        List<Honor> list = honorService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * 查询【微信查询个人信息】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:honor:list')" )
    @GetMapping("/appletList" )
    public TableDataInfo appletList(String openId) {
        List<Honor> list = honorService.findByopenId(openId);
        Iterator iterator = list.iterator();
        List<RestVo> restVos = new ArrayList<>();
        while (iterator.hasNext()){
            Honor honor1 =  (Honor)iterator.next();
            RestVo restVo = (RestVo)honor1.clone();
            restVos.add(restVo);
        }
        return getDataTable(restVos);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:honor:export')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Honor honor) {
        QueryWrapper<Honor> queryWrapper = new QueryWrapper<Honor>(honor);
        List<Honor> list = honorService.list(queryWrapper);
        ExcelUtil<Honor> util = new ExcelUtil<Honor>(Honor. class);
        return util.exportExcel(list, "honor" );
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:honor:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(honorService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:honor:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Honor honor) {
        return toAjax(honorService.save(honor) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:honor:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Honor honor) {
        return toAjax(honorService.updateById(honor) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:honor:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(honorService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
